package com.zhou.piledriver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.piledriver.exception.CustomerException;
import com.zhou.piledriver.mapper.PdProjectMapper;
import com.zhou.piledriver.model.entity.PdProject;
import com.zhou.piledriver.model.so.ProjectSO;
import com.zhou.piledriver.model.so.SimpleSo;
import com.zhou.piledriver.model.vo.PdProjectVO;
import com.zhou.piledriver.service.PdProjectService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhouShenBin
 * @date 2024/3/27 11:40
 * @description
 */
@Service
public class PdProjectServiceImpl implements PdProjectService {

    @Resource
    PdProjectMapper pdProjectMapper;

    @Override
    public IPage<PdProjectVO> page(Page<PdProject> page, ProjectSO so) {
        return pdProjectMapper.pageProject(page,so);
    }

    @Override
    public boolean create(PdProject bo) {
        LambdaQueryWrapper<PdProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PdProject::getName, bo.getName());
        long exist = pdProjectMapper.selectCount(queryWrapper);
        if (exist > 0) {
            throw new CustomerException("项目名称已存在");
        }
        return pdProjectMapper.insert(bo) > 0;
    }

    @Override
    public List<PdProject> list(String tenantId) {
        LambdaQueryWrapper<PdProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(tenantId),PdProject::getTenantId, tenantId);
        return pdProjectMapper.selectList(queryWrapper);
    }
}
